//
//  HYInternetViewModel.m
//  XPlayer
//
//  Created by liguoqing on 2023/8/3.
//

#import "HYInternetViewModel.h"
#import "HYPreviewHistoryManager.h"

@interface HYInternetViewModel ()

@property (nonatomic, strong) NSArray *dataSource;

@property (nonatomic, strong) NSMutableArray *historySource;

@end

@implementation HYInternetViewModel

- (instancetype)init
{
    self = [super init];
    if(self){
        self.clickSubject = [RACSubject subject];
        self.refreshSubject = [RACSubject subject];
        self.isHiddenDelete = YES;
        [self loadData];
    }
    return self;
}

- (void)loadData
{
    HYInternetItemViewModel *baiDuVM = [[HYInternetItemViewModel alloc]init];
    baiDuVM.type = HYInternetType_BaiDu;
    baiDuVM.iconName = @"icon_baidu";
    baiDuVM.url = @"https://www.baidu.com";
    baiDuVM.title = NSLocalizedString(@"百度", nil);
    
    HYInternetItemViewModel *cloudVM = [[HYInternetItemViewModel alloc]init];
    cloudVM.type = HYInternetType_BaiDuCloud;
    cloudVM.iconName = @"icon_baiduyun";
    cloudVM.title = NSLocalizedString(@"百度云盘", nil);
    
    HYInternetItemViewModel *movieVM = [[HYInternetItemViewModel alloc]init];
    movieVM.type = HYInternetType_Moive;
    movieVM.iconName = @"icon_movie";
    movieVM.url = @"https://v.qq.com";
    movieVM.title = NSLocalizedString(@"视频", nil);
    
    HYInternetItemViewModel *musicVM = [[HYInternetItemViewModel alloc]init];
    musicVM.type = HYInternetType_Music;
    musicVM.iconName = @"icon_music";
    musicVM.title = NSLocalizedString(@"音乐", nil);
    musicVM.url = @"https://music.163.com";
    self.dataSource = @[baiDuVM,cloudVM,movieVM,musicVM];
}

- (void)reloadHistoryRecord
{
    self.historySource = [NSMutableArray arrayWithArray:[HYPreviewHistoryManager fetchRecentPlayRecord]];
    self.isHiddenDelete = self.historySource.count?NO:YES;
    [self.updatedContentSignal sendNext:nil];
}

- (NSInteger)numberOfItemsIsHeader:(BOOL)isHeader
{
    if(isHeader){
        return self.dataSource.count;
    }
    return self.historySource.count;
}

- (NSString *)headerTitleForHeaderInSection:(NSInteger)section
{
    return NSLocalizedString(@"最近播放", nil);;
}

- (id)cellForRowAtIndexPath:(NSIndexPath *)indexPath isHeader:(BOOL)isHeader
{
    if(isHeader){
        return self.dataSource[indexPath.row];
    }
    return self.historySource[indexPath.row];
}

- (void)didSelectItemAtIndexPath:(NSIndexPath *)indexPath isHeader:(BOOL)isHeader
{
    if(isHeader){
        HYInternetItemViewModel *itemVM = self.dataSource[indexPath.item];
        [self.clickSubject sendNext:itemVM];
    }
}

- (void)removeCurrenHistoryAtIndexPath:(NSIndexPath *)indexPath
{
    [self.historySource removeObjectAtIndex:indexPath.row];
}

- (BOOL)checkVideoPlayUrl:(NSString *)videoUrl
{
    //WMV,AVI,MKV,RMVB,RM,XVID,MP4,3GP,MPG,MOV,m3u8
    if([videoUrl containsString:@".wmv"] || [videoUrl containsString:@".avi"] || [videoUrl containsString:@".mkv"] || [videoUrl containsString:@".rmvb"] || [videoUrl containsString:@".rm"] || [videoUrl containsString:@".xvid"] || [videoUrl containsString:@".mp4"] || [videoUrl containsString:@".3gp"] || [videoUrl containsString:@".mpg"] || [videoUrl containsString:@".mov"] || [videoUrl containsString:@".m3u8"]){
        return YES;
    }
    return NO;
}

@end
